#!/bin/bash
default_hostname=master
read -t 15 -p "请在15秒内设置主机名[master]: " hostname
if [ -z $hostname ];then
        hostname=$default_hostname
fi
hostnamectl hostname $hostname

echo "关闭防火墙"
systemctl --now disable firewalld

echo "关闭Swap分区"
swapoff -a
sed -i "/\bswap\b/s/^/#/" /etc/fstab

echo "配置hosts"
echo `hostname -I |cut -d' ' -f1` `hostname` >> /etc/hosts

echo "禁用SELinux"
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

echo "安装Docker-CE"
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
systemctl --now enable docker

echo "安装cri-dockerd"
curl --output-dir /tmp -Os  https://mirror.ghproxy.com/https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.12/cri-dockerd-0.3.12.amd64.tgz
tar -xf /tmp/cri-dockerd-0.3.12.amd64.tgz -C /tmp
cp /tmp/cri-dockerd/cri-dockerd /usr/bin/
curl --output-dir /etc/systemd/system/ -Os  "https://raw.githubusercontent.com/Mirantis/cri-dockerd/master/packaging/systemd/cri-docker.{service,socket}"
systemctl --now enable cri-docker.socket

echo "修改内核参数"
sysctl -w net.ipv4.ip_forward=1 >> /etc/sysctl.d/k8s.conf

echo "配置kubernetes阿里云yum源"
cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.29/rpm/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.29/rpm/repodata/repomd.xml.key
EOF

echo "配置IPVS"
yum -y -q install ipvsadm
echo "ip_vs ip_vs_rr ip_vs_wrr ip_vs_sh nf_conntrack" | xargs -n1 modprobe
cat <<EOF > /etc/modules-load.d/ipvs.conf
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack
EOF
lsmod | grep ip_vs

echo "安装kubeadm、kubectl、kubelet"
yum -y -q install kubeadm kubectl kubelet
systemctl --now enable kubelet

echo "下载k8s阿里云镜像"
kubeadm config images pull --cri-socket unix:///var/run/cri-dockerd.sock --image-repository=registry.aliyuncs.com/google_containers
echo "重命名k8s官方镜像"
docker images --format {{.Repository}}:{{.Tag}} | grep aliyuncs | grep -v coredns | awk -F '/' '{print $NF}' |xargs -I% docker tag registry.aliyuncs.com/google_containers/% registry.k8s.io/%
docker images --format {{.Repository}}:{{.Tag}} | grep aliyuncs | grep  coredns | awk -F '/' '{print $NF}' |xargs -I% docker tag registry.aliyuncs.com/google_containers/% registry.k8s.io/coredns/%
docker images --format {{.Repository}}:{{.Tag}} | grep aliyuncs |xargs docker rmi

echo "导出k8s镜像"
docker images --format {{.Repository}}:{{.Tag}} | grep k8s |xargs docker save |gzip > k8s_img.tar.gz

echo "初始化k8s集群"
kubeadm init --apiserver-advertise-address `hostname -I | cut -d' ' -f1` --node-name `hostname` --pod-network-cidr 10.244.0.0/16 --cri-socket unix:///var/run/cri-dockerd.sock
  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config
 export KUBECONFIG=/etc/kubernetes/admin.conf

echo "安装Calico网络插件"
kubectl apply -f https://mirror.ghproxy.com/https://raw.githubusercontent.com/projectcalico/calico/master/manifests/calico.yaml

echo "配置命令补全"
yum -y -q install bash-completion
echo "source <(kubeadm completion bash)" >> ~/.bashrc
echo "source <(kubectl completion bash)" >> ~/.bashrc
source ~/.bashrc
